﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SixthClassAssignment.DAL;

namespace SixthClassAssignment{
    public partial class ClsWindow : Form{
        private int _selectIndex = -1;
        private void InitGrip(){
            dataGrid .SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
        
        private void ShowClass(){
            List<Class> classes = SqlDao.SelectClassList();
            classes.Add(new Class("", "", ""));
            dataGrid.DataSource = classes;
            dataGrid.Columns["No"].HeaderText = "班号";
            dataGrid.Columns["Name"].HeaderText = "班名";
            dataGrid.Columns["SchNo"].HeaderText = "学校";
        }
        
        private void DataGrid_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e){
            if (e.Button==MouseButtons.Right) {
                if (e.RowIndex>=0) {
                    _selectIndex = e.RowIndex;
                    dataGrid.ClearSelection();//清空选择项
                    dataGrid.Rows[e.RowIndex].Selected = true;//获取被选中行索引
                    dataGrid.CurrentCell = dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    contexMenuStrip.Show(MousePosition.X, MousePosition.Y);//弹出操作菜单
                }
            }
        }

        private void Delete_Click(object sender,EventArgs e){
            var no = dataGrid.Rows[_selectIndex].Cells[0].Value.ToString();
            if (!"".Equals(no)) {
                var count = SqlDao.DeleteClassByNo(no);
                if (count == 1) {
                    ShowClass();
                    SqlDao.InsertLog(new Log(DateTime.Now.ToString(), "delete class: " + no));

                }
            }
        }

        private void Insert_Click(object sender, EventArgs e){
            if (_selectIndex == dataGrid.Rows.Count-1) {
                var cells = dataGrid.Rows[_selectIndex].Cells;
                var no = cells[0].Value.ToString();
                var name = cells[1].Value.ToString();
                var sno = cells[2].Value.ToString();
                if (!"".Equals(no)) {
                    Class cls = new Class(no, name, sno);
                    int count = SqlDao.InsertClass(cls);
                    if (count == 1) {
                        ShowClass();
                        SqlDao.InsertLog(new Log(DateTime.Now.ToString(), "insert class" + cls));
                    }
                }
            }
        }

        private void Update_Click(object sender, EventArgs e){
            if (_selectIndex != dataGrid.Rows.Count-1) {
                var cells = dataGrid.Rows[_selectIndex].Cells;
                var no = cells[0].Value.ToString();
                var name = cells[1].Value.ToString();
                var sno = cells[2].Value.ToString();
                if (!"".Equals(no)) {
                    Class cls = new Class(no, name, sno);
                    int count = SqlDao.UpdateClass(cls);
                    if (count == 1) {
                        ShowClass();
                        SqlDao.InsertLog(new Log(DateTime.Now.ToString(), "update class" + cls));

                    }
                }
            }
        }
        private void Form_Closing(object sender, FormClosingEventArgs e){
            Hide();
            var window = new FunctionWindow("root");
            window.Show();
        }

        private void AddEvent(){
            dataGrid.CellMouseClick += DataGrid_CellMouseUp;
            deleteToolStripMenuItem.Click += Delete_Click;
            insertToolStripMenuItem.Click += Insert_Click;
            updateToolStripMenuItem.Click += Update_Click;
            this.FormClosing += Form_Closing;
        }
        
        public ClsWindow(){
            InitializeComponent();
            InitGrip();
            ShowClass();
            AddEvent();
        }
    }
}